package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.Permission.PermissionItem;
import com.groupseven.backend.domain.Role.Role;
import com.groupseven.backend.domain.Sidebar.*;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.mapper.AuthoritiesMapper;
import com.groupseven.backend.mapper.UserMapper;
import com.groupseven.backend.services.AuthoritiesService;
import com.groupseven.backend.test.DataEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class AuthoritiesServiceImpl implements AuthoritiesService {

    @Autowired
    private AuthoritiesMapper authoritiesMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DataEncode dataEncode;

    /**
     * @Author 王志华
     * @param user 用户类型
     * @return
     */
    public List<SidebarItems> getSidebar(User user){
        if(user.getUsertype()==1){
            return authoritiesMapper.AdminSidebar();
        } else if (user.getUsertype()==0) {
            return authoritiesMapper.UserSidebar();
        }
        return authoritiesMapper.UserSidebar();
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public List<SidebarItems> getSubSidebar(User user){
        if(user.getUsertype()==1){
            return authoritiesMapper.AdminSubSidebar();
        } else if (user.getUsertype()==0) {
            return authoritiesMapper.UserSubSidebar();
        }
        return authoritiesMapper.UserSubSidebar();
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public MySidebar getMySidebar(User user){
        return authoritiesMapper.getMySidebar(user);
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<SidebarMap> getSidebarMap(){
        return authoritiesMapper.getSidebarMap();
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<SubSidebarMap> getSubSidebarMap(){
        return authoritiesMapper.getSubSidebarMap();
    }

    /**
     * @Author 王志华
     * @param mysidebar
     * @return
     */
    public int modifyMySidebar(MySidebar mysidebar){
        return authoritiesMapper.modifyMySidebar(mysidebar);
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public List<String> getMyOperationAuthorities(User user){
        return authoritiesMapper.getMyOperationAuthorities(user);
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public Set<String> getPermissionsByUsername (User user){
        return authoritiesMapper.getPermissionsByUsername(user.getUsername());
    }

//    /**
//     * @Author 王志华
//     * @param permissions
//     * @return
//     */
//    public int updateUserPermissions(Set<Permission> permissions, String username){
//        int x = 0;
//        for (Permission i : permissions){
//            if(authoritiesMapper.updateUserPermission(i.getPermissionNumber(),username) == 1){
//                x++;
//            }
//        }
//        return x;
//    }

    /**
     * @Author 王志华
     * @param Username
     * @return
     */
    public int resetUserPermissions(String Username){
        if(authoritiesMapper.resetUserPermission(Username)==1){
            return 1;
        }
        return 0;
    }

    /**
     * @Author 王志华
     * @param Rolenumber
     * @return
     */
    public List<PermissionItem> getParentPermissiontreeByRolenumber(int Rolenumber){
        return authoritiesMapper.getParentPermissiontreeByRolenumber(Rolenumber);
    }

    /**
     * @Author 王志华
     * @param Rolenumber
     * @return
     */
    public List<PermissionItem> getSubPermissiontreeByRolenumber(int Rolenumber){
        return authoritiesMapper.getSubPermissiontreeByRolenumber(Rolenumber);
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<PermissionItem> getParentPermissions(){
        return authoritiesMapper.getParentPermissions();
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<PermissionItem> getSubPermissions(){
        return authoritiesMapper.getSubPermissions();
    }

    /**
     * @Author 王志华
     * @param Rolenumber
     * @param Permissions
     * @return
     */
    public int updateRolePermission(int Rolenumber,int[] Permissions){
        long begintime = System.currentTimeMillis();
        if (authoritiesMapper.deletePermissionsByRole(Rolenumber)>0){
            long endtime=System.currentTimeMillis();
            long costTime = (endtime - begintime);
            System.out.println("删除耗时："+costTime);
            int x = 0;
            begintime = System.currentTimeMillis();
            for(int Iterator : Permissions){
                if(authoritiesMapper.addPermissionByRoleAndPermid(Rolenumber,Iterator)==1){
                    x++;
                }
            }
            endtime=System.currentTimeMillis();
            costTime = (endtime - begintime);
            System.out.println("插入耗时："+costTime);
            return x;
        }
        return 0;
    }

    /**
     * @Author 王志华
     * @param Rolenumber
     * @param Permissions
     * @return
     */
    public int updateRolePermissions(int Rolenumber, Map<String,Object> Permissions){
        long begintime = System.currentTimeMillis();
        if (authoritiesMapper.deletePermissionsByRole(Rolenumber)>=0){
            long endtime=System.currentTimeMillis();
            long costTime = (endtime - begintime);
            System.out.println("删除耗时："+costTime);
            begintime = System.currentTimeMillis();
            System.out.println(Permissions);
            int x = authoritiesMapper.addPermissionsByRoleAndPermid(Permissions);
            System.out.println("x的值："+x);
            endtime=System.currentTimeMillis();
            costTime = (endtime - begintime);
            System.out.println("插入耗时："+costTime);
            return x;
        }
        return 0;
    }

    /**
     * @Author 王志华
     * @param Usertype
     * @return
     */
    public Set<String> getPermissionsByUsertype(int Usertype){
        return authoritiesMapper.getPermissionsByUsertype(Usertype);
    }

    /**
     * @Author 王志华
     * @param roles
     * @return
     */
    public boolean multi_deleteByRolenumber(List<Role> roles){
        List<Integer> deleteList = new ArrayList<>();
        for(Role Iterator : roles){
            deleteList.add(Iterator.getRolenumber());
        }
        Map<String,Object> query = new HashMap<>();
        query.put("id",deleteList);
        if(authoritiesMapper.multi_deleteByRolenumber(query)==0){
            return false;
        }
        return true;
    }

    public Set<String> getPermissionsByRole (User user){
        return authoritiesMapper.getPermissionsByRole(user.getUsertype());
    }

}
